<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Clients extends CI_Controller
{

	var $data;

	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('tank_auth');
		$this->data=array();
	}

	function _sendData(){
		$this->output->set_header("Content-type: application/json");
		$this->output->set_output(json_encode($this->data));
	}

	function _remap($method, $params = array())
	{
		$this->load->model('clientmodel');
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		} else {
			$this->profiles=$this->tank_auth->get_profile();
			$isOwner=false;
			foreach($this->profiles as $row){
				if($row->type==="Owner"){
					$isOwner=true;
				}
			}
			if ($isOwner) {
				if (method_exists($this, $method))
				{
					return call_user_func_array(array($this, $method), $params);
				}
			}
		}
		show_404(); // Either means that the user isn't authorized or that the page doesn't exist.
	}

	function index()
	{
		show_404();
	}

	function get($oid=false){
		if(!$oid){
			$oid=$this->input->get_post('oid');
		}
		if($oid){ // Return a single Client object.
			$this->data['data'] = $this->clientmodel->getByClientId($oid);
			$this->_sendData();
		} else { // Return all Client objects for the logged-in Owner.
			$page = 1; // The current page
			$sortname = 'id'; // Sort column
			$sortorder = 'asc'; // Sort order
			$qtype = ''; // Search column
			$query = ''; // Search string
			$rp = 15; // Rows per page
			if($this->input->get_post('page')){
				$page=$this->input->get_post('page');
			}
			if($this->input->get_post('sortname')){
				$sortname=$this->input->get_post('sortname');
			}
			if($this->input->get_post('sortorder')){
				$sortorder=$this->input->get_post('sortorder');
			}
			if($this->input->get_post('qtype')){
				$qtype=$this->input->get_post('qtype');
			}
			if($this->input->get_post('query')){
				$query=$this->input->get_post('query');
			}
			if($this->input->get_post('rp')){
				$rp=$this->input->get_post('rp');
			}
			$this->data['page']=$page;
			$this->data['total']=$this->clientmodel->getCountByOwnerId($this->tank_auth->get_user_id(),$qtype,$query);
			$this->data['rows']=array();
			$queryData = $this->clientmodel->getAllByOwnerId($this->tank_auth->get_user_id(),$sortname,$sortorder,$qtype,$query,$page,$rp);
			foreach($queryData as $row){
				$row->balance = number_format($row->balance,2);
				$this->data['rows'][]=array(
					'id'=>$row->id,
					'cell'=>$row
				);
			}
			$this->_sendData();
		}
	}

	function save($act='add',$oid=false){
		try{
			$act=$this->input->get_post('act',false,$act);
			$data = array();
			//$data['owner_id'] = $this->tank_auth->get_user_id();
			//$data['profile_id'] = $this->tank_auth->get_profile_id();
			$data['name'] = $this->input->get_post('name',false,'');
			$data['company_name'] = $this->input->get_post('company_name',false,'');
			$data['email_address'] = $this->input->get_post('email_address',false,'');
			$data['mailing_address'] = $this->input->get_post('mailing_address',false,'');
			if($act==='add'){
				$data['profile_id'] = $this->tank_auth->get_profile_id();
				$data = $this->clientmodel->addClient($data);
				$site_name = $this->config->item('website_name', 'tank_auth');
				$owner=$this->users->get_user_by_id($this->tank_auth->get_user_id());
				$owner_profile=$this->users->get_profile($data['profile_id']);
				$owner_profile=$owner_profile[0];
				//print_r($owner_profile);
				$viewData=array(
					'owner_name'=>$owner_profile->name,
					'site_name'=>$site_name,
					'company_name'=>$owner_profile->company_name,
					'uuid'=>$data['uuid']
					
				);
				$this->tank_auth->_send_email('invite', $data['email_address'], $viewData);
				$this->load->library('email');
				$this->email->print_debugger();
			} else if($act === 'mod'){
				if(!$oid){
					$oid=$this->input->get_post('profile_id',false,false);
					
				}
				if($oid){
					$this->data['status']='success';
					$this->data['msg']='Client updated successfully.';
					$this->_sendData();
					return null;
				} else {
					$this->data['status']='fail';
					$this->data['msg']='A Client Id must be specified!';
					$this->_sendData();
					return null;
				}
			}
			$this->data['status']='success';
			$this->data['msg'] = 'Client '.$oid.' successfully saved!';
		} catch (Exception $x) {
			$this->data['status']='fail';
			$this->data['msg'] = $x->getMessage();
			$this->data['exception']=$x;
			log_message('error',$x);
		}
		$this->_sendData();
	}
	


}

